<template>
  <!-- 精选题库 -->
  <div class="container">
    <!-- 提示 新增 -->
    <div class="prompt">
      <el-alert
        title="说明：目前支持学科和关键字条件筛选"
        type="success"
        :closable="false"
      >
      </el-alert>
      <el-button
        icon="el-icon-edit"
        size="small"
        type="success"
        @click="$router.push('new')"
      >
        新增用户
      </el-button>
    </div>
    <!-- 提示 新增 -->
    <!-- 搜索 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="学科">
        <el-input v-model="formInline.user" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="二级目录">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="无数据" value="shanghai"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input v-model="formInline.user" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="试题类型">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难度">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方向">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="录入人">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目备注">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业简称">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="city" label="城市">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="searchFor">
        <el-button size="small" @click="submit">清除</el-button>
        <el-button
          size="small"
          type="primary"
          @click="onSubmit(formInline.user)"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 搜索 -->
    <!-- table 表格数据区域 -->
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="全部" name="first">全部</el-tab-pane>
      <el-tab-pane label="待审核" name="second">待审核</el-tab-pane>
      <el-tab-pane label="已审核" name="third">已审核</el-tab-pane>
      <el-tab-pane label="已拒绝" name="fourth">已拒绝</el-tab-pane>
    </el-tabs>

    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ total }} 条记录</span>
    </div>
    <!-- 记录 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="120">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="120">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" width="120">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" width="120">
      </el-table-column>
      <el-table-column prop="question" label="题干" width="280">
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180">
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" width="80">
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="120">
      </el-table-column>
      <el-table-column prop="chkState" label="审核状态" width="120">
      </el-table-column>
      <el-table-column prop="chkRemarks" label="审核意见" width="150">
      </el-table-column>
      <el-table-column prop="chkUser" label="审核人" width="120">
        <template #default="{ row }">
          <span>{{ row.chkUser ? '待审核' : '已审核'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="publishState" label="发布状态" width="150">
        <template #default="{ row }">
          <span>{{ row.publishState ? '已发布' : '已下架' }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template #default="{ row }">
          <el-button @click="openPreview(row.id)" type="text" size="small">
            预览
          </el-button>
          <el-button type="text" size="small" @click="openCheck(row.id)" :disabled="row.chkUser ? false : true">
            审核
          </el-button>
          <el-button
            type="text"
            size="small"
            :disabled="row.publishState ? true : false"
            @click="$router.push(`new/${row.id}`)"
          >
            修改
          </el-button>
          <el-button type="text" size="small" @click="openDisabled(row)">
            {{ row.publishState ? '下架' : '上架' }}
          </el-button>
          <el-button
            type="text"
            size="small"
            :disabled="row.publishState ? true : false"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->
    <!-- 分页组件区域 -->
    <div class="block">
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          :current-page="queryObj.page"
          :page-size="queryObj.pageSize"
          layout="prev, pager, next, sizes, jumper"
          :page-sizes="[10, 20, 30, 50]"
          background
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 分页组件区域 -->
    <!-- 弹窗组件 -->
    <Preview
      ref="preview"
      :dialogFormVisible.sync="dialogVisible"
      :id="currentId"
    ></Preview>
    <QuestionsCheck
      ref="questionsCheck"
      :dialogFormVisible.sync="visibleCheck"
      :id="checkId"
    ></QuestionsCheck>
    <!-- 弹窗组件 -->
  </div>
</template>

<script>
import { choice, choicePublish } from '@/api/hmmm/questions.js'
import Preview from '../components/questions-preview.vue'
import QuestionsCheck from '../components/questions-check.vue'

export default {
  components: {
    Preview,
    QuestionsCheck
  },
  data () {
    return {
      formInline: {},
      list: [],
      queryObj: {
        page: 1, // 控制当前页
        pageSize: 10 // 控制每一页有几条数据
      },
      dialogVisible: false, // 打开添加
      visibleCheck: false, // 打开审核
      loading: false,
      choiceState: 1,
      currentId: 0,
      checkId: 0,
      total: 0, // 总记录数
      activeName: 'first'
    }
  },
  created () {
    this.choiceList()
  },
  methods: {
    // 打开预览
    async openPreview (id) {
      this.dialogVisible = true // 显示弹层
      this.currentId = id
      console.log(id)

      // 因为vue更新是异步，在props的currentdId修改之前，已经创建了子组件发送了请求
      this.$nextTick(() => {
        // 调用子组件上的方法
        this.$refs.preview.getDepartmentInfo(id)
      })
    },
    // 打开审核
    async openCheck (id) {
      this.visibleCheck = true // 显示弹层
      this.checkId = id

      // 因为vue更新是异步，在props的currentdId修改之前，已经创建了子组件发送了请求
      this.$nextTick(() => {
        // 调用子组件上的方法
        this.$refs.questionsCheck.getDepartmentInfo()
      })
    },
    // 获取数据
    async choiceList () {
      this.loading = true

      const { data } = await choice(this.queryObj)
      console.log(data)

      this.list = [...data.items]
      this.total = data.counts

      this.loading = false
    },
    // 更改状态
    async openDisabled (row) {
      console.log(row)
      const res = { ...row }
      if (res.publishState) {
        res.publishState = 0
      } else {
        res.publishState = 1
      }
      const { id, publishState } = res

      await choicePublish({ id, publishState })

      this.initTable()
    },
    // 每页数量变化时触发
    handleSizeChange (pagesize) {
      // console.log(`每页 ${pagesize} 条`)

      this.queryObj.pageSize = pagesize

      this.choiceList()
    },
    // 当前页变化时触发
    handleCurrentChange (page) {
      // console.log(`当前页: ${page}`)

      this.queryObj.page = page

      this.choiceList()
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.formInline.region = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user, region) {
      // console.log(this.list.title)
      // console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.title)
        return item.title === user || item.state === region
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 10px;
  .prompt {
    display: flex;
    margin-bottom: 15px;
    .el-alert--success.is-light {
      font-size: 12px;
      color: pink;
      background-color: #fff;
    }
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-form-item {
      display: flex;
      width: 25%;
      margin-right: 0;
      ::v-deep .el-form-item__content {
        display: flex;
        justify-content: space-between;
        width: 80%;
        .el-select {
          width: 100%;
          display: flex;
          justify-content: space-between;
          ::v-deep .el-input__inner {
            height: 32px;
            width: 100%;
            line-height: 32px;
            font-size: 14px;
          }
        }
      }
    }
    ::v-deep .city {
      display: flex;
      width: 25%;
      margin-right: 0;
      ::v-deep .el-form-item__content {
        display: flex;
        justify-content: space-between;
        width: 80%;
        ::v-deep .el-select {
          width: 48%;
          ::v-deep .el-input {
            height: 32px;
            line-height: 32px;
            width: 100%;
            margin-right: 10px;
            font-size: 14px;
          }
        }
      }
    }
    ::v-deep .searchFor {
      display: flex;
      justify-content: flex-end;
      width: 25%;
      padding-left: 134px;
      .el-button {
        margin-left: 10px;
      }
    }
  }
}
</style>
